package com.exam.student.controller;


import com.exam.anno.MyLog;
import com.exam.constant.MessageConstant;
import com.exam.context.UserUtils;
import com.exam.entity.*;
import com.exam.entity.dto.ForgotDto;
import com.exam.entity.dto.RegisterDto;
import com.exam.entity.vo.MessageVo;
import com.exam.entity.vo.ReplayVo;
import com.exam.student.service.LoginService;
import com.exam.util.AliOssUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
public class CommonController {

    @Autowired
    private LoginService loginService;

    @Autowired
    private AliOssUtil aliOssUtil;

    /**
     * 登录操作
     *
     * @param login
     * @return
     */
    @MyLog
    @PostMapping("/exams/login")
    public ApiResult login(@RequestBody Login login) {
        return loginService.login(login);
    }

    /**
     * 退出登录
     * @param exitForm
     * @return
     */
    @MyLog
    @PostMapping("/exit")
    public ApiResult exit(@RequestBody ExitForm exitForm){
        return loginService.exit(exitForm);
    }

    /**
     * 注册操作
     *
     * @param registerDto
     * @return
     */
    @MyLog
    @PostMapping("/exams/register")
    public ApiResult register(@RequestBody RegisterDto registerDto) {
        return loginService.register(registerDto);
    }

    /**
     * 找回密码
     *
     * @param forgotDto
     * @return
     */
    @MyLog
    @PostMapping("/exams/forgotPwd")
    public ApiResult forgotPwd(@RequestBody ForgotDto forgotDto) {
        return loginService.forgotPwd(forgotDto);
    }

    /**
     * 试卷列表分页
     *
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/exams/{page}/{size}")
    public ApiResult findAll(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        return loginService.examSelectPage(page, size);
    }

    /**
     * 关键字搜索
     *
     * @param key
     * @return
     */
    @GetMapping("/exams")
    public ApiResult findBySource(@RequestParam("key") String key) {
        ExamManage examManage = new ExamManage();
        examManage.setSource(key);
        return loginService.findBySource(examManage);
    }

    /**
     * 查询课程，对课程描述
     *
     * @param examCode
     * @return
     */

    @GetMapping("/exam/{examCode}")
    public ApiResult findById(@PathVariable("examCode") Long examCode) {

        ExamManage examManage = new ExamManage();
        examManage.setExamCode(examCode);
        return loginService.findBySource(examManage);
    }

    /**
     * 查询详细试卷
     *
     * @param paperId
     * @return
     */
//    @MyLog
    @GetMapping("/paper/{paperId}")
    public ApiResult findQuestion(@PathVariable("paperId") Long paperId) {
        return loginService.findQuestion(paperId);
    }

    /**
     * 将学生的分数插入进去
     *
     * @param score
     */
    @MyLog
    @PostMapping("/score")
    public ApiResult insertScore(@RequestBody Score score) {
        ApiResult apiResult = loginService.insertScore(score);
        return apiResult;
    }

    /**
     * 查询我的分数
     *
     * @param page
     * @param size
     * @param examCode
     * @return
     */
    @GetMapping("/score/{page}/{size}/{examCode}")
    public ApiResult findMyScore(@PathVariable("page") Integer page, @PathVariable("size") Integer size,
                                 @PathVariable("examCode") Long examCode) {
        return loginService.findMyScore(page, size, examCode);
    }

    /**
     * 留言消息分页
     *
     * @param page
     * @param size
     * @return
     */
//    @MyLog
    @GetMapping("/messages/{page}/{size}")
    public ApiResult findAllMessage(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        return loginService.findAllMessage(page, size);
    }

    /**
     * 插入留言消息
     *
     * @param messageVo
     */
    @MyLog
    @PostMapping("/message")
    public ApiResult insertMessage(@RequestBody MessageVo messageVo) {
        ApiResult apiResult = loginService.insertMessage(messageVo);
        return apiResult;
    }

    /**
     * 插入评论消息
     *
     * @param replayVo
     */
    @MyLog
    @PostMapping("/replay")
    public ApiResult insertReplay(@RequestBody ReplayVo replayVo) {
        ApiResult apiResult = loginService.insertReplay(replayVo);
        return apiResult;
    }

    /**
     * 删除评论消息
     *
     * @param replayVo
     * @return
     */
    @MyLog
    @DeleteMapping("/replayDelete")
    public ApiResult replayDelete(@RequestBody ReplayVo replayVo) {
        ApiResult apiResult = loginService.replayDelete(replayVo);
        return apiResult;
    }

    /**
     * 删除留言消息
     *
     * @param
     * @return
     */
    @MyLog
    @DeleteMapping("/deleteMessage")
    public ApiResult deleteMessage(@RequestParam("id") Integer id) {
        MessageVo messageVo = new MessageVo();
        messageVo.setId(id);
        ApiResult apiResult = loginService.deleteMessage(messageVo);
        return apiResult;
    }

    /**
     * 学生修改密码
     *
     * @param updatePwdDto
     * @return
     */
    @MyLog
    @PutMapping("/studentPWD")
    public ApiResult updatePWD(@RequestBody RegisterDto updatePwdDto) {
        return loginService.updatePwdDto(updatePwdDto);
    }

    /**
     * 文件上传到oss
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public ApiResult upload(MultipartFile file){
        ApiResult apiResult = new ApiResult();
        try {
            //原始文件名
            String originalFilename = file.getOriginalFilename();
            //截取原始文件名的后缀   dfdfdf.png
            String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
            //构造新文件名称
            String objectName = UUID.randomUUID().toString() + extension;

            //文件的请求路径
            String filePath = aliOssUtil.upload(file.getBytes(), objectName);
            apiResult.setCode(200);
            apiResult.setData(filePath);
            apiResult.setMessage("上传成功");
//            System.out.println("filePath: " + filePath);
            return apiResult;
        } catch (IOException e) {
            log.error("文件上传失败：{}", e);
        }
        return apiResult;
    }

    /**
     * 删除oss里面图片
     * @param filePath
     * @return
     */
    @DeleteMapping("/deleteAvatar")
    public ApiResult deleteAvatar(@RequestParam("filePath") String filePath){
        return loginService.deleteAvatar(filePath);
    }

    /**
     * 将头像存储在DB
     * @param avatar
     * @return
     */
    @PostMapping("/insertAvatar")
    public ApiResult insertAvatar(@RequestBody Avatar avatar) {
        return loginService.insertAvatar(avatar);
    }

    /**
     * 进入页面根据id查找头像信息
     * @param userId
     * @return
     */
    @GetMapping("/findByIdAvatars")
    public ApiResult findByIdAvatars(@RequestParam("userId") Long userId) {
        return loginService.findByIdAvatars(userId);
    }

    /**
     * 登录信息写到缓存
     * @param userId
     * @return
     */
    @GetMapping("/findByIdRedis")
    public ApiResult findByIdLiveCount(@RequestParam("userId") Long userId) {
        return loginService.findByIdLiveCount(userId);
    }

}
